import { consoleRequest } from '@core/utils/request';

export default class Console {
  static fetchPlatformList = () => {
    return consoleRequest.get<
      any,
      { count: number, results: Array<ParentPlatform> }
    >('/ctcloud/console/console_v3_index_platform_list/v3/index/platform/list/');
  };

  static pinyinStartRanges = ['A-G', 'H-K', 'L-S', 'T-Z'];

  static getPlatformPinyinGroups = async () => {
    const platformParents = (await Console.fetchPlatformList()).results;
    const groups = Console.pinyinStartRanges.map((range) => {
      return {
        pinyinRange: range,
        platformParents: [] as Array<ParentPlatform>,
        pinyinRegExp: new RegExp(`[${range}]`),
      };
    });
    platformParents.forEach((platformParent) => {
      groups.forEach((group, groupIndex) => {
        if (group.pinyinRegExp.test(platformParent.parentPinyin_start.toUpperCase())) {
          groups[groupIndex].platformParents.push(platformParent);
        }
      });
    });
    return groups;
  };

  static fetchMe = () => {
    return consoleRequest.get<any, ConsoleUser>('/ctcloud/console/console_v3_rest_me/v3/rest/me/');
  };

  static fetchPermission = () => {
    return consoleRequest.get<any, { platform: Platform }>(
      '/ctcloud/console/console_v3_index_promission/v3/index/promission/',
    );
  };
}

export enum ServiceStatus {
  Disabled = '0',
  Enabled = '1',
  Limited = '2',
}
export interface PlatformDetail {
  hasKms?: ServiceStatus,
  hasWaf?: ServiceStatus,
  security_safe?: ServiceStatus,
  hasCtwt?: ServiceStatus,
}
export interface Platform {
  ctyun?: number,
  details?: Record<string, any>,
  isSale: number,
  name?: string,
  orderZoneType: string,
  parentName: string,
  type: number,
  uuid?: string,
  zoneId: string,
  zoneName: string,
  zoneType: string,
  zoneUrl: string,
}
export type Region = Platform;
export interface ParentPlatform {
  parentName: string,
  parentPinyin: string,
  parentPinyin_start: string,
  platforms: Array<Platform>,
}
export type ParentRegion = ParentPlatform;
export interface ConsoleUser {
  account_id: string,
  create_date: string,
  email: string,
  user_id: string,
  username: string,
}
